using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.UI;
using System.IO;

namespace BlogControl
{
    public class ArchiveManager
    {
        Blog _control;
        public ArchiveManager(Blog control)
        {
            this._control = control;
        }

        
        public string GetDatesFilter(Blog _control)
        {
            if (_control.CalendarID == null) return null;

            if (CurrentCalendar == null) return null;
            if (CurrentCalendar.SelectedDate == DateTime.MinValue) return null;
            return CurrentCalendar.SelectedDate.ToString("yyyy-MM-dd");
        }
        private Calendar CurrentCalendar
        {
            get
            {
               return _control.Page.FindControl(_control.CalendarID) as Calendar;
            }
        }
        internal void RenderCalendar(string calendarID)
        {
            if (string.IsNullOrEmpty(calendarID)) throw new ArgumentNullException("CalendarID");
            JavascriptManager.AddSelectedByDateFunction(_control.Buffer);
            if (CurrentCalendar == null) throw new ArgumentException("Could not find a control with type \"Calendar\" and id \"" + calendarID + "\"");
            CurrentCalendar.DayRender += new DayRenderEventHandler(c_DayRender);
        }

        private void c_DayRender(object sender, DayRenderEventArgs e)
        {
            if (_control.PostsDates.Contains(e.Day.Date.ToString("yyyy-MM-dd")))
            {
                e.Cell.BackColor = Color.Blue;
                
                e.Cell.Attributes.Add("onclick", "selectPostsByDate('" + e.Day.Date.ToString("yyyy-MM-dd") + "');");
            }
        }
    }
}
